package com.xjh.t.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjh.t.common.DataGridView;
import com.xjh.t.common.ResultObj;
import com.xjh.t.common.WebUtils;
import com.xjh.t.domain.Notice;
import com.xjh.t.domain.User;
import com.xjh.t.service.INoticeService;
import com.xjh.t.vo.NoticeVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-25
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    @RequestMapping("loadAllNotice")
    public DataGridView loadAllNotice(NoticeVo vo)
    {
        IPage<Notice> page = new Page<>(vo.getPage(),vo.getLimit());
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(vo.getTitle()),"title",vo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(vo.getOpername()),"opername",vo.getOpername());
        queryWrapper.ge(vo.getStartTime() != null,"createtime",vo.getStartTime());
        queryWrapper.le(vo.getEndTime() != null,"createtime",vo.getEndTime());

        queryWrapper.orderByDesc("createtime");
        this.noticeService.page(page,queryWrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }
    @RequestMapping("addNotice")
    public ResultObj addNotice(NoticeVo vo)
    {
        try {
            vo.setCreatetime(new Date());
            User user = (User) WebUtils.getSession().getAttribute("user");
            vo.setOpername(user.getName());
            this.noticeService.save(vo);
            return ResultObj.ADD_SUCESS;

        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.ADD_ERROR;
        }
    }
    @RequestMapping("updateNotice")
    public ResultObj updateNotice(NoticeVo vo)
    {
        try {

            this.noticeService.updateById(vo);
            return ResultObj.ADD_SUCESS;

        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.ADD_ERROR;
        }
    }
    @RequestMapping("deleteNotice")
    public ResultObj deleteNotice(Integer id)
    {
        try {

            this.noticeService.removeById(id);
            return ResultObj.DELETE_SUCESS;

        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.DELETE_ERROR;
        }
    }
    @RequestMapping("batchDeleteNotice")
    public ResultObj batchDeleteNotice(NoticeVo vo)
    {
        try {

            this.noticeService.removeByIds(Arrays.asList(vo.getIds()));
            return ResultObj.DELETE_SUCESS;

        }
        catch (Exception e)
        {
            e.printStackTrace();;
            return ResultObj.DELETE_ERROR;
        }
    }
}

